package com.tangyuan.service.impl;

import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.common.util.StringUtils;
import com.tangyuan.dto.UserActionDto;
import com.tangyuan.feignapis.user.IUserActionFeignClient;
import com.tangyuan.model.Movies;
import com.tangyuan.model.UserAction;
import com.tangyuan.service.MovieService;
import com.tangyuan.service.UserActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserActionServiceImpl implements UserActionService {

    @Autowired
    IUserActionFeignClient userActionFeignClient;
    @Autowired
    MovieService movieService;
    @Override
    public void view(UserAction userAction) {
        userActionFeignClient.view(userAction);
    }

    @Override
    public Result<String> getUserAction(Long userId, Long movieId, Float grade, Float collection, Float thump) {
        Movies movie = movieService.getMoviesById(movieId);
        if(movie == null){
            return Result.fail(ResultCodeEnum.NO_DATA.getMessage());
        }
        UserActionDto built = UserActionDto.builder()
                .userId(userId)
                .movieId(movieId)
                .categoryId(StringUtils.isEmpty(movie.getCategoryId()) ? 0 : Integer.parseInt(movie.getCategoryId()))
                .grade(grade)
                .collection(collection)
                .thump(thump).build();
        return userActionFeignClient.getUserAction(built);
    }
}
